package br.com.almanach.seguranca;

import br.com.almanach.dominio.seguranca.Papel;
import br.com.almanach.dominio.seguranca.Perfil;
import br.com.almanach.dominio.seguranca.Usuario;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

public class AlmanachUserDetails implements UserDetails {

    private Usuario usuario;

    public AlmanachUserDetails(Usuario usuario) {
        this.usuario = usuario;
    }

    public Collection<GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> papeis = new HashSet<GrantedAuthority>();
        for (Perfil perfil : usuario.getPerfis()) {
            for (Papel papel : perfil.getPapeis()) {
                papeis.add(new GrantedAuthorityImpl(papel.getNome()));
            }
        }
        return papeis;
    }

    public String getPassword() {
        return usuario.getSenha();
    }

    public String getUsername() {
        return usuario.getNome();
    }

    public boolean isAccountNonExpired() {
        return true;
    }

    public boolean isAccountNonLocked() {
        return true;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return true;
    }

}